﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZWZ.Web.WebAdmin.Update
{
    public partial class UpdateUG : System.Web.UI.Page
    {
        protected ModelBase.User_Group OldGroup;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定模型
                string id = Request["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    OldGroup = DBProvider.DB.Ctx.User_Group.Where(i => i.ID == id).FirstOrDefault();
                    if (!string.IsNullOrEmpty(OldGroup.PowerString)) { UserPower1.SetPower(new Codes.UserPower(OldGroup.PowerString)); }
                }
                if (OldGroup != null) TxtName.Text = OldGroup.Name;
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Codes.MessageBox MsgBox = new Codes.MessageBox(this);
            string id = Request["id"];
            if (!string.IsNullOrEmpty(id))
            {
                //修改
                var oldgroup = DBProvider.DB.Ctx.User_Group.Where(i => i.ID == id).FirstOrDefault();
                if (oldgroup == null) { Response.Write("该用户组不存在，可能已被删除！"); Response.End(); }
                oldgroup.Name = TxtName.Text;
                oldgroup.Type = Enums.User.GroupType.用户.ToString();
                oldgroup.PowerString = UserPower1.GetPowerString();
            }
            else
            {
                //新建
                var newgroup = new ModelBase.User_Group();
                newgroup.ID = "UG" + Codes.IDHelper.CreateTimeID();
                newgroup.Name = TxtName.Text;
                newgroup.Type = Enums.User.GroupType.用户.ToString();
                newgroup.PowerString = UserPower1.GetPowerString();
                DBProvider.DB.Ctx.User_Group.AddObject(newgroup);
            }
            DBProvider.DB.Ctx.SaveChanges();
            MsgBox.Show("保存成功！").RefrashOpener().CloseWindow().Send();
        }
    }
}